﻿package com.edward.service;


import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.TransferList;
import com.edward.model.User;


public class TransferListManager extends HibernateDaoSupport {

	public void addTransferList(TransferList transferList) {
		
		getHibernateTemplate().save(transferList);
	}
	
	public TransferList getTransferList(int id){
		TransferList t =(TransferList)getHibernateTemplate().get(TransferList.class, id);
		return t;
	}
	

	
	@SuppressWarnings("unchecked")
	public List<TransferList> listTransferList(boolean oversea) {
		if(oversea){
			return getHibernateTemplate().find("from TransferList t where t.warehouse in ('E','F','G')");
		}else{
			return getHibernateTemplate().find("from TransferList t where t.warehouse in ('A','B','C')");
		}
	}

	
	
	public void updateTransferList(TransferList transferList) {
	
		getHibernateTemplate().update(transferList);
	}
	
	public void deleteTransferList(int id){
		
		getHibernateTemplate().delete(getTransferList(id));
	}

	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,boolean oversea) {
		List <TransferList>list;
		if(oversea){
			list = getHibernateTemplate().find("from TransferList t where t.warehouse in ('E','F','G')"  );
		}else{
			list = getHibernateTemplate().find("from TransferList t where t.warehouse in ('A','B','C')" );
		}
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <TransferList>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	//自动添加对应表格的单号
	@SuppressWarnings("unchecked")	
	public String listNum(){
		List <TransferList>list = getHibernateTemplate().find("from TransferList");
		int i = list.size();
		if(i==0){
			return "000001";
		}else{
			String tmp = list.get(i-1).getListNum();
			while(tmp.indexOf(0) == '0'){
				tmp = tmp.substring(1);
			}
			int t = Integer.parseInt(tmp) + 1;
			tmp = t + "";
			for(int j=6;j>tmp.length();){
				tmp ="0"+tmp;
			}
			return tmp;
		}
		
		
	}
	
	@SuppressWarnings("unchecked")
	public String getUserName(String name){
		List <User>list = getHibernateTemplate().find("from User u where u.username = '" + name +"'" );
		return list.get(0).getDescn();
		
	}

}



